<!-- 我的帖子页面 -->
<template>
	<view>
		<view class="my-giveup-items-box">
			<block v-for="item in myGiveUpDataList" :key="item.id">
				<release-item :itemObj="item" @starClick="orderItemStarClick" @deleteClick="orderItemDeleteClick"></release-item>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sortData: {
					page: 1,
					page_size: 10,
					is_self: 1,
				},
				// 模拟的我的帖子数据列表
				myGiveUpDataList: []
			};
		},
		onLoad() {
			this.getData()
		},
		onShow() {
			// 判断是否在帖子详情页删除了帖子，如果删除了 则 vuex_caseDeleteID 会有被删除的帖子的 id
			if(this.vuex_caseDeleteID) {
				let index = this.userDataList.findIndex(item => item.id == this.vuex_caseDeleteID)
				if(index != -1) {
					this.userDataList.splice(index, 1)		// 移除这条在详情页已经被删除了的数据
					this.$u.vuex('vuex_caseDeleteID', '')	// 将 vuex 中存储的被删除的帖子的 id 清空
				}
			}
		},
		async onPullDownRefresh() {
			this.sortData.page = 1
			this.myGiveUpDataList = []
			await this.getData()
			uni.stopPullDownRefresh()
			this.$u.toast('页面数据已刷新')
		},
		onReachBottom() {
			this.sortData.page += 1
			this.getData()
		},
		methods: {
			async getData() {
				const res = await this.$u.api.getPostList(this.sortData)
				if(!res) return
				this.myGiveUpDataList = [...this.myGiveUpDataList, ...res.list.data]
				console.log(this.myGiveUpDataList);
			},
			// 点赞事件
			orderItemStarClick(id) {
				let index = this.myGiveUpDataList.findIndex(item => item.id === id)
				this.myGiveUpDataList[index].has_star = !this.myGiveUpDataList[index].has_star
				if(this.myGiveUpDataList[index].has_star) {		// 判断点赞数是该 +1 还是 -1
					this.myGiveUpDataList[index].star_num += 1
				} else {
					this.myGiveUpDataList[index].star_num -= 1
				}
			},
			// 删除了帖子
			orderItemDeleteClick(id) {
				let index = this.myGiveUpDataList.findIndex(item => item.id === id)
				this.myGiveUpDataList.splice(index, 1)	// 将这项从数据列表中删除
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-giveup-items-box {
		min-height: calc(100vh - 20rpx);
		background-color: #F4F6FA;
	}
</style>
